<?php
/**
 *  @copyright 2010
 *  @package _version@
 *  @author oriotiori
 */
class Admin_Model_Seo extends Zend_Db_Table_Abstract
{
	protected $_name = 'admin_seo';

	/**
	 * Método para Crear SEO
	 * @param array $seo
	 * @return mixed $res
	 */
	function createSeo($seo)
	{
		try{
			$data = array(
                    'Title'         => $seo['Title'],
                    'Keywords'      => $seo['Keywords'],
                    'Creation_Date' => new Zend_Db_Expr('NOW()'),
                    'Distribution'  => $seo['Distribution'],
                    'Description'   => $seo['Description'],
                    'Modified_By'   => $seo['ModifiedBy'],
                    'Subject'       => $seo['Subject'],
                    'Robots'        => $seo['Robots'],
                    'Revisit'       => $seo['Revisit'],
                    'Language'      => $seo['Language'],
                    'City'          => $seo['City'],
                    'Country'       => $seo['Country']
			);

			$res = $this->getAdapter()->insert(TABLE_PREFIX.$this->_name,$data);
			return $res;
		}
		catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
	}


	/**
	 * Actualizar SEO
	 * @param array $seo
	 * @return mixed $res
	 */
	function updateSeo($seo)
	{
		try
		{

			$data = array(
                    'Title'         => $seo['Title'],
                    'Keywords'      => $seo['Keywords'],
                    'Distribution'  => $seo['Distribution'],
                    'Description'   => $seo['Description'],
                    'Modified_By'   => $seo['Modified_By'],
                    'Subject'       => $seo['Subject'],
                    'Robots'        => $seo['Robots'],
                    'Revisit'       => $seo['Revisit'],
                    'Language'      => $seo['Language'],
                    'City'          => $seo['City'],
                    'Country'       => $seo['Country']
			);

			$where[]="Id=".$seo['Id'];
			$res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name, $data, $where);

			return $res;
		}
		catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
	}


	/**
	 *
	 * Método para leer los datos de un SEO
	 * @param mixed $seo
	 * @return mixed $control
	 */
	function readSeo($seo)
	{
		$TOSeo = new Admin_Model_TO_Seo();
		$x_id = $seo['Id'];

		if($x_id >0){
			 
			$select= $this->getAdapter()->select()->from(TABLE_PREFIX.$this->_name)->where('Id=?', $x_id)->where('Status=?', '0')->limit(1);

			$data = $this->getAdapter()->fetchRow($select);
			$rowCount = count($data);

			try{

				if ($rowCount > 0)
				{
					$TOSeo->setId($data['Id']);
					$TOSeo->setTitle($data['Title']);
					$TOSeo->setDescription($data['Description']);
					$TOSeo->setDistribution($data['Distribution']);
					$TOSeo->setKeywords($data['Keywords']);
					$TOSeo->setSubject($data['Subject']);
					$TOSeo->setRobots($data['Robots']);
					$TOSeo->setRevisit($data['Revisit']);
					$TOSeo->setLanguage($data['Language']);
					$TOSeo->setCity($data['City']);
					$TOSeo->setCountry($data['Country']);

					$control = $TOSeo->getAllVars();
					return $control;
				}
				else
				{
					throw new Exception('No found results.');
				}
			}
			catch(Exception $e)
			{
				echo 'Caught exception: ',  $e->getMessage(), "\n";
			}
		}
	}


	function readAllSeo()
	{
		$TOSeo = new Admin_Model_TO_Seo();

		try{
			$select= $this->getAdapter()->select()->from(TABLE_PREFIX.$this->_name)->where('Status=?', '0')->order('Id', 'DESC');
			$data = $this->getAdapter()->fetchAll($select);

			foreach ($data as $dt)
			{
				$TOSeo->setId($dt['Id']);
				$TOSeo->setTitle($dt['Title']);
				$TOSeo->setSubject($dt['Subject']);
				$TOSeo->setDescription($dt['Description']);
				$TOSeo->setDistribution($dt['Distribution']);
				$TOSeo->setKeywords($dt['Keywords']);
				$TOSeo->setCity($dt['City']);
				$TOSeo->setLanguage($dt['Language']);
				$TOSeo->setCountry($dt['Country']);
				$TOSeo->setRevisit($dt['Revisit']);
				$TOSeo->setRobots($dt['Robots']);
				$TOSeo->add();
			}

			$datao = $TOSeo->getAllData();
			return $datao;

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
	}


	function deleteSeo($seo)
	{
		$data = array('Status'	=>  '1','Modified_By' 	=>  $seo->getModifiedBy());
		$where[]="Id=".$seo->getId();

		try{
			$res = $this->getAdapter()->update(TABLE_PREFIX.$this->_name, $data, $where);
			return $res;
		}
		catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
	}
}